import { Injectable } from '@nestjs/common';
import { InjectModel } from '@nestjs/mongoose';
import { Model } from 'mongoose';
import { GetListDto } from 'src/interface';
import { User, UserDocument } from 'src/models/user';

@Injectable()
export class UserService {
  constructor(
    @InjectModel('User') private readonly userModule: Model<UserDocument>,
  ) {}
  /* 查询有多少条数据 */
  async queryCount(): Promise<User[] | number> {
    return await this.userModule.find().estimatedDocumentCount();
  }
  /* 查询所有数据 */
  async findAll(): Promise<User[] | null> {
    return await this.userModule.find().sort({ sort: 1 });
  }
  /* 查询数据 */
  async findList(getListDto: GetListDto): Promise<User[] | null> {
    return await this.userModule
      .find()
      .skip((getListDto.pageIndex - 1) * getListDto.pageSize)
      .limit(getListDto.pageSize)
      .sort({ sort: 1 });
  }
  /* 添加数据 */
  async createData(createDto: User): Promise<User> {
    const createdUser = new this.userModule(createDto);
    return await createdUser.save();
  }
  /* 查询单条数据 */
  async findOne(id: string): Promise<any> {
    return await this.userModule.findById(id);
  }
  /* 更新数据 */
  async updateData(id: string, upDataUserDto: User): Promise<any> {
    return await this.userModule.findByIdAndUpdate(id, upDataUserDto);
  }
  /* 删除数据 */
  async removeData(id: string): Promise<any> {
    return await this.userModule.findByIdAndDelete(id);
  }
  /* 批量删除数据 */
  async deleteData(deleteMenusDto: string[]): Promise<any> {
    return await this.userModule.deleteMany({ _id: { $in: deleteMenusDto } });
  }
}
